package com.googlecode.jsimplemarket.wicket.components.grid;

import com.googlecode.jsimplemarket.repository.GenericProperty;
import com.googlecode.jsimplemarket.repository.GenericRestriction;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Alexandru.Codescu
 */
public class FilterToolbar<T extends Serializable> extends AbstractToolbar {
    private FilterView filterView;
    private GridDataProvider<T> dataProvider;
    private DataTable dataTable;
    private Grid grid;
    private List<ColumnModel> gridColumns;

    public FilterToolbar(DataTable dataTable, List<ColumnModel> gridColumns, GridDataProvider<T> dataProvider, Grid grid) {
        super(dataTable);
        this.dataTable = dataTable;
        this.dataProvider = dataProvider;
        this.grid = grid;
        this.gridColumns = new ArrayList<ColumnModel>(gridColumns);
        init();
    }

    private class FilterView extends ListView<ColumnModel> {

        public FilterView(String id, List<ColumnModel> gridColumns) {
            super(id, gridColumns);
        }

        @Override
        protected void populateItem(ListItem<ColumnModel> li) {
            final ColumnModel columnModel = li.getModelObject();
            TextField txtValue = new TextField("test", new PropertyModel(columnModel, "value"),columnModel.getType());
            if(columnModel.getType().getName().equals(Date.class.getName())){
            	txtValue.add(new DatePicker());
            }
            txtValue.add(new AjaxFormComponentUpdatingBehavior("onchange") {
                @Override
                protected void onUpdate(AjaxRequestTarget art) {
                    if (columnModel.getType().getName().equals(String.class.getName())) {
                        GenericProperty property = GenericProperty.getInstance(columnModel.getProperty());
                        GenericRestriction restriction = new GenericRestriction(property, columnModel.getValue(), GenericRestriction.Op.LK);
                        if(columnModel.getValue()==null) {
                       	 dataProvider.removeRestriction(restriction);
                        } else {
                       	 dataProvider.addRestriction(restriction);
                        }
                        dataTable.modelChanged();
                        art.add(grid);
                    }
                    else{
                    	 GenericProperty property = GenericProperty.getInstance(columnModel.getProperty());
                         GenericRestriction restriction = new GenericRestriction(property, columnModel.getValue(), GenericRestriction.Op.EQ);
                         if(columnModel.getValue()==null) {
                        	 dataProvider.removeRestriction(restriction);
                         } else {
                        	 dataProvider.addRestriction(restriction);
                         }
                         dataTable.modelChanged();
                         art.add(grid);
                    }
                }
            });
            li.add(txtValue);
        }
    }

    private void init() {
        filterView = new FilterView("filters", gridColumns);
        add(filterView);
    }
    
    public void setColumns(List<ColumnModel> gridColumns) {
        this.gridColumns.clear();
        this.gridColumns.addAll(gridColumns);
        filterView.modelChanged();
    }
}
